<template>
  <el-dialog
    title="编辑"
    :visible.sync="$attrs.visableIf"
    @close="handleCloseDialog"
    width="600px"
    append-to-body
    class="borderb_dia"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px"
      label-position="left"
    >
      <el-form-item label="用户名" prop="name">
        <el-input
          v-model="form.name"
          maxlength="20"
          show-word-limit
          placeholder="请输入用户名"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="form.password"
          placeholder="请输入密码"
          show-password
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="Email" prop="email">
        <el-input v-model="form.email" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="登录时间" prop="">
        <span>{{ rowitem.last_login_time || "-" }}</span>
      </el-form-item>
      <el-form-item label="注册时间" prop="">
        <span>{{ rowitem.create_time || "-" }}</span>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">暂停</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="footer-btn">
      <el-button @click="handleReset">重置</el-button>
      <el-button type="primary" @click="submitForm">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { validPhone, validPassword } from "@/utils/validate";
import { editInfo } from "@/api/advmaster";
export default {
  data() {
    return {
      form: {
        id: "",
        name: "",
        phone: "",
        email: "",
        status: "",
        password: "",
      },
      // 表单校验
      rules: {
        name: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        email: [
          {
            required: true,
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: "blur",
          },
        ],
        phone: [{ required: true, validator: validPhone, trigger: "blur" }],
        password: [
          { required: true, validator: validPassword, trigger: "blur" },
        ],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
    };
  },
  props: {
    rowitem: {
      default: null,
    },
  },
  created() {
    this.form.id = this.rowitem.custom.id;
    this.form.name = this.rowitem.custom.user_login;
    this.form.phone = this.rowitem.custom.mobile;
    this.form.email = this.rowitem.custom.email;
    this.form.password = this.rowitem.custom.password;
    this.form.status = this.rowitem.custom.status;
  },
  methods: {
    //重置表单
    handleReset() {
      this.resetForm("form");
    },

    handleCloseDialog() {
      this.$emit("update:visableIf", false);
    },

    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            editInfo(this.form).then(() => {
              this.$modal.msgSuccess('修改成功')
              this.handleCloseDialog();
              this.$emit('getList');
            })
          }
        }
      })
    },
  },
};
</script>

<style scoped lang="scss">
.operate-Big {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 80px;
}

.group-right {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 15px;
}
</style>
